package adminuser

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type AdminUserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 管理员详情
func NewAdminUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUserInfoLogic {
	return &AdminUserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminUserInfoLogic) AdminUserInfo(req *types.AdminUserInfoRequest) (resp *types.AdminUserInfoResponse, err error) {
	id := headInfo.GetTokenUid(l.ctx)
	if id == 0 {
		return nil, code.AdminUserTokenInvalid
	}

	infoResp, err := l.svcCtx.UserServiceRPC.AdminUserInfo(l.ctx, &userrpc.AdminUserInfoRequest{Id: id})
	if err != nil {
		logc.Error(l.ctx, "AdminUserInfo err: ", err)
		return nil, err
	}

	roleNameResp, err := l.svcCtx.AuthRoleRpc.GetRoleInfo(l.ctx, &v1.GetRoleInfoRequest{
		Id: int64(infoResp.AdminUser.Role),
	})
	if err != nil {
		logc.Error(l.ctx, "GetRoleInfo err: ", err)
		return nil, err
	}

	resp = &types.AdminUserInfoResponse{
		Id:              infoResp.AdminUser.Id,
		Account:         infoResp.AdminUser.Account,
		Password:        infoResp.AdminUser.Password,
		Phone:           infoResp.AdminUser.Phone,
		PhoneCode:       infoResp.AdminUser.PhoneCode,
		Nickname:        infoResp.AdminUser.Nickname,
		Email:           infoResp.AdminUser.Email,
		Role:            infoResp.AdminUser.Role,
		RoleName:        roleNameResp.RoleName,
		RegisterAt:      infoResp.AdminUser.RegisterAt,
		LoginAt:         infoResp.AdminUser.LoginAt,
		LoginIp:         infoResp.AdminUser.LoginIp,
		LoginNumber:     infoResp.AdminUser.LoginNumber,
		UserStatus:      infoResp.AdminUser.UserStatus,
		LoginStatus:     infoResp.AdminUser.LoginStatus,
		GoogleCode:      infoResp.AdminUser.GoogleCode,
		IsOpenGoogle:    infoResp.AdminUser.IsOpenGoogle,
		IsBindingGoogle: infoResp.AdminUser.IsBindingGoogle,
	}
	return
}
